package com.wcar.store.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcar.common.entity.FrontUser;
import com.wcar.common.entity.StoreEvaluate;
import com.wcar.common.entity.User;
import com.wcar.common.entity.UserRole;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.store.dao.EvaluateDao;
import com.wcar.store.dao.FrontUserDao;
import com.wcar.store.dao.UserDao;
import com.wcar.store.dao.UserRoleDao;
import com.wcar.store.service.EvaluateService;
import com.wcar.store.vo.EvaluateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: four-wcar-idea
 * @BelongsPackage: com.wcar.store.service.impl
 * @Author: 李睢康
 * @CreateTime: 2024-05-27  15:26
 */
@Service
public class EvaluateServiceImpl implements EvaluateService {
    @Resource
    private EvaluateDao evaluateDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private UserDao userDao;
    @Resource
    private FrontUserDao frontUserDao;
    @Resource
    private UserRoleDao userRoleDao;
    @Override
    public Result<IPage> selectAll(EvaluateVo evaluateVo) {
        IPage<StoreEvaluate> iPage=new Page<>(evaluateVo.getCurrentPage(),evaluateVo.getPageSize());
        QueryWrapper<StoreEvaluate> wrapper=new QueryWrapper<>();
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        QueryWrapper<UserRole> wrapper1=new QueryWrapper<>();
        wrapper1.eq("user_id",user.getUserId());
        List<UserRole> userRoles = userRoleDao.selectList(wrapper1);
        ArrayList<Integer> roleIds=new ArrayList<>();
        for (UserRole u: userRoles) {
            roleIds.add(u.getRoleId());
        }
        if(evaluateVo.getStoreName()!=null&&evaluateVo.getStoreName()!=""){
            wrapper.like("ts.store_name",evaluateVo.getStoreName());
        }
        if(evaluateVo.getUserName()!=null&&evaluateVo.getUserName()!=""){
            wrapper.like("tfu.user_name",evaluateVo.getUserName());
        }
        if(!roleIds.contains(1)){
            wrapper.eq("tsu.user_id",user.getUserId());
        }
        IPage<StoreEvaluate> iPage1 = evaluateDao.selectAll(iPage,wrapper);
        return new Result<IPage>(200,"获取门店评价成功",iPage1);
    }

    @Override
    public Result<List<FrontUser>> selectUserAll() {
        List<FrontUser> frontUsers = frontUserDao.selectList(null);
        return new Result<List<FrontUser>>(200,"查询所有用户成功",frontUsers);
    }

    @Override
    public Result<String> insert(StoreEvaluate evaluate) {
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        evaluate.setEvaluateTime(format);
        int insert = evaluateDao.insert(evaluate);
        return new Result<String>(200,"添加评价成功", insert+"") ;
    }

    @Override
    public Result<String> update(StoreEvaluate evaluate) {
        int i = evaluateDao.updateById(evaluate);
        return new Result<String>(200,"修改评价成功", i+"") ;
    }

    @Override
    public Result<Integer> delete(Integer evaluateId) {
        int i = evaluateDao.deleteById(evaluateId);
        return new Result<Integer>(200,"删除成功",i);
    }

    @Override
    public Result<List<StoreEvaluate>> selectComment(Integer storeId) {
        QueryWrapper<StoreEvaluate> wrapper=new QueryWrapper<>();
        wrapper.eq("store_id",storeId);
        List<StoreEvaluate> storeEvaluates = evaluateDao.selectList(wrapper);
        return new Result<>(200,"根据id查询评价成功",storeEvaluates);
    }

    @Override
    public Result<String> insertEvaluate(StoreEvaluate evaluate) {
        System.out.println("evaluate = " + evaluate);
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        evaluate.setEvaluateTime(format);
        int insert = evaluateDao.insert(evaluate);
        if (insert>0){
            return new Result<String>(200,"添加评价成功",null);
        }
        return new Result<>(500,"添加失败",null);
    }

}
